package com.bwie.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.bwie.auth.service.AuthService;
import com.bwie.common.domain.User;
import com.bwie.common.domain.request.UserReq;
import com.bwie.common.domain.response.UserRes;
import com.bwie.common.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Log4j2
public class AuthController {
    @Autowired
    private AuthService authService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 通过手机号获取验证码
     * @param phone
     * @return
     */
    @PostMapping("/getCode/{phone}")
    public Result getCode(@PathVariable String phone){
        log.info("功能：通过手机号获取验证码，请求方式：{}，请求路径：{}，请求参数：{}",request.getMethod(),request.getRequestURI(),phone);
        authService.getCode(phone);
        Result result = Result.success();
        log.info("功能：通过手机号获取验证码，请求方式：{}，请求路径：{}，响应结果：{}",request.getMethod(),request.getRequestURI(), JSONObject.toJSONString(result));
        return result;
    }

    /**
     * 手机号验证码登录
     * @param userReq
     * @return
     */
    @PostMapping("/phoneLogin")
    public Result<UserRes> phoneLogin(@RequestBody UserReq userReq){
        log.info("功能：手机号验证码登录，请求方式：{}，请求路径：{}，请求参数：{}",request.getMethod(),request.getRequestURI(),JSONObject.toJSONString(userReq));
        UserRes userRes = authService.phoneLogin(userReq);
        Result<UserRes> result = Result.success(userRes);
        log.info("功能：手机号验证码登录，请求方式：{}，请求路径：{}，响应结果：{}",request.getMethod(),request.getRequestURI(), JSONObject.toJSONString(userReq));
        return result;
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/user/info")
    public Result<User> userInfo(){
        log.info("功能：获取用户信息，请求方式：{}，请求路径：{}",request.getMethod(),request.getRequestURI());
        User user = authService.userInfo();
        Result<User> result = Result.success(user);
        log.info("功能：手机号验证码登录，请求方式：{}，请求路径：{}，响应结果：{}",request.getMethod(),request.getRequestURI(), JSONObject.toJSONString(result));
        return result;
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public Result logout(){
        log.info("功能：退出登录，请求方式：{}，请求路径：{}",request.getMethod(),request.getRequestURI());
        authService.logout();
        Result result = Result.success();
        log.info("功能：退出登录，请求方式：{}，请求路径：{}，响应结果：{}",request.getMethod(),request.getRequestURI(),JSONObject.toJSONString(result));
        return result;
    }
}
